home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.06 Jun 91 / Fancy Dialogs Code / Dialog Globals.p next >
Encoding:
Text File  |  1991-03-18  |  6.9 KB  |  309 lines  |  [TEXT/PJMM]

  1. { Dialog globals unit. }
  2.  
  3. { Written by Thomas Engel, M.D. }
  4. { Copyright © 1991 MacTutor. }
  5.  
  6.  
  7. unit Globals;
  8.  
  9. interface
  10.  
  11.     const
  12.  
  13.         { Special Characters. }
  14.  
  15.         EnterCh = Chr(3);
  16.         BackSpaceCh = Chr(8);
  17.         TabCh = Chr(9);
  18.         ReturnCh = Chr(13);
  19.  
  20.         { Dialog resource IDs. }
  21.  
  22.         AboutID = 128;
  23.         MessageID = 129;
  24.         StringID = 130;
  25.         YesNoID = 131;
  26.         YesNoCancelID = 132;
  27.         FontSizeID = 133;
  28.         PageMarginsID = 134;
  29.  
  30.         { Dialog item numbers. }
  31.  
  32.         OKButton = 1;
  33.         CancelButton = 2;
  34.         YesButton = 1;
  35.         NoButton = 3;
  36.  
  37.         { Miscellaneous. }
  38.  
  39.         ControlOn = 1;
  40.         ControlOff = 0;
  41.         ControlActive = 0;
  42.         ControlInactive = 255;
  43.         FlashTicks = 10;
  44.         AfterTicks = 2;
  45.         IBeamID = 1;
  46.         WatchID = 4;
  47.  
  48.     type
  49.         IntegerPtr = ^Integer;
  50.         IntegerHandle = ^IntegerPtr;
  51.         LongintPtr = ^Longint;
  52.         LongintHandle = ^LongintPtr;
  53.  
  54.         MeasureOption = (inches, cm, points);
  55.  
  56.         MarginRecord = record
  57.                 left, top, right, bottom: Single;
  58.                 measure: MeasureOption;
  59.             end;
  60.  
  61.         DialogItemSet = set of 0..255;
  62.  
  63.         TDialog = object
  64.                 toolboxDialog: DialogPtr;
  65.                 toolboxDPeek: DialogPeek;
  66.                 itemCount: Integer;
  67.                 defaultItem, cancelItem, defaultText: Integer;
  68.                 dismissSet: DialogItemSet;
  69.                 procedure IDialog (dialogID: Integer; default, cancel, text: Integer; dismiss: DialogItemSet; centered: Boolean);
  70.                 procedure Free;
  71.                 procedure Center;
  72.                 function Show: Integer;
  73.                 procedure Hide;
  74.                 function EventFilter (var theEvent: EventRecord; var itemHit: Integer): Boolean;
  75.                 procedure FixCursor;
  76.                 procedure Command (ch: Char; var itemHit: Integer);
  77.                 procedure Hit (var itemHit: Integer);
  78.                 procedure Draw;
  79.                 procedure DrawDefault;
  80.                 procedure DrawBox (itemSet: DialogItemSet; margin, thickness: Integer);
  81.                 procedure DrawTitleBox (itemSet: DialogItemSet; margin, thickness: Integer; title: Str255);
  82.                 procedure FlashItem (item: Integer);
  83.                 function GetText (item: Integer): Str255;
  84.                 procedure SetText (item: Integer; text: Str255);
  85.                 function GetInteger (item: Integer): Longint;
  86.                 procedure SetInteger (item: Integer; value: Longint);
  87.                 function GetReal (item: Integer): Extended;
  88.                 procedure SetReal (item: Integer; value: Extended; places: Integer);
  89.                 function GetControlValue (item: Integer): Integer;
  90.                 procedure SetControlValue (item: Integer; value: Integer);
  91.                 function GetControlTitle (item: Integer): Str255;
  92.                 procedure SetControlTitle (item: Integer; title: Str255);
  93.                 function GetRadioItem (radioSet: DialogItemSet): Integer;
  94.                 procedure SetRadioItem (radioSet: DialogItemSet; itemSelected: Integer);
  95.                 procedure SetIcon (item: Integer; iconID: Integer);
  96.                 procedure SetPicture (item: Integer; pictureID: Integer);
  97.                 procedure SetUserItem (item: Integer; userProc: ProcPtr);
  98.             end;
  99.  
  100.         TAboutDialog = object(TDialog)
  101.                 procedure IAboutDialog;
  102.                 function Show: Integer;
  103.                 override;
  104.             end;
  105.  
  106.         TMessageDialog = object(TDialog)
  107.                 procedure IMessageDialog (message: Str255; iconID: Integer);
  108.                 procedure SetMessage (message: Str255);
  109.             end;
  110.  
  111.         TStringDialog = object(TDialog)
  112.                 procedure IStringDialog (prompt, default: Str255);
  113.                 procedure SetData (prompt, default: Str255);
  114.                 procedure GetData (var response: Str255);
  115.             end;
  116.  
  117.         TYesNoDialog = object(TDialog)
  118.                 procedure IYesNoDialog (prompt: Str255; default: Integer);
  119.                 procedure SetPrompt (prompt: Str255);
  120.             end;
  121.  
  122.         TYesNoCancelDialog = object(TDialog)
  123.                 procedure IYesNoCancelDialog (prompt: Str255; default: Integer);
  124.                 procedure SetPrompt (prompt: Str255);
  125.             end;
  126.  
  127.         TMarginsDialog = object(TDialog)
  128.                 measureSet: DialogItemSet;
  129.                 procedure IMarginsDialog (defaultMargins: MarginRecord);
  130.                 procedure Draw;
  131.                 override;
  132.                 procedure Hit (var itemHit: Integer);
  133.                 override;
  134.                 procedure SetData (newMargins: MarginRecord);
  135.                 procedure GetData (var theMargins: MarginRecord);
  136.             end;
  137.  
  138.         TFontSizeDialog = object(TDialog)
  139.                 procedure IFontSizeDialog (defaultSize: Integer);
  140.                 procedure SetData (fontSize: Integer);
  141.                 procedure GetData (var fontSize: Integer);
  142.             end;
  143.  
  144.     var
  145.         IBeam, Watch: CursHandle;
  146.  
  147.  
  148.     procedure ShowAbout;
  149.     procedure ShowMessage (message: Str255);
  150.     function YesNoDialog (prompt: Str255; defaultButton: Integer): Integer;
  151.     function YesNoCancelDialog (prompt: Str255; defaultButton: Integer): Integer;
  152.     function StringDialog (prompt: Str255; var response: Str255): Boolean;
  153.     function FontSizeDialog (var fontSize: Integer): Boolean;
  154.     function MarginsDialog (var margins: MarginRecord): Boolean;
  155.  
  156.  
  157. implementation
  158.  
  159.  
  160.     procedure ShowAbout;
  161.  
  162.     { Show the application About… dialog. }
  163.  
  164.         var
  165.             theDialog: TAboutDialog;
  166.             theItem: Integer;
  167.  
  168.     begin
  169.         New(theDialog);
  170.         if theDialog <> nil then
  171.             begin
  172.                 theDialog.IAboutDialog;
  173.                 theItem := theDialog.Show;
  174.                 theDialog.Free
  175.             end
  176.     end;
  177.  
  178.  
  179.     procedure ShowMessage (message: Str255);
  180.  
  181.     { Show the message dialog. }
  182.  
  183.         var
  184.             theDialog: TMessageDialog;
  185.             theItem: Integer;
  186.  
  187.     begin
  188.         New(theDialog);
  189.         if theDialog <> nil then
  190.             begin
  191.                 theDialog.IMessageDialog(message, 1);
  192.                 theItem := theDialog.Show;
  193.                 theDialog.Free
  194.             end
  195.     end;
  196.  
  197.  
  198.     function YesNoDialog (prompt: Str255; defaultButton: Integer): Integer;
  199.  
  200.     { Ask a yes or no question. }
  201.  
  202.         var
  203.             theDialog: TYesNoDialog;
  204.  
  205.     begin
  206.         New(theDialog);
  207.         if theDialog <> nil then
  208.             begin
  209.                 theDialog.IYesNoDialog(prompt, defaultButton);
  210.                 YesNoDialog := theDialog.Show;
  211.                 theDialog.Free
  212.             end
  213.     end;
  214.  
  215.  
  216.     function YesNoCancelDialog (prompt: Str255; defaultButton: Integer): Integer;
  217.  
  218.     { Ask a yes, no or cancel question. }
  219.  
  220.         var
  221.             theDialog: TYesNoCancelDialog;
  222.  
  223.     begin
  224.         New(theDialog);
  225.         if theDialog <> nil then
  226.             begin
  227.                 theDialog.IYesNoCancelDialog(prompt, defaultButton);
  228.                 YesNoCancelDialog := theDialog.Show;
  229.                 theDialog.Free
  230.             end
  231.     end;
  232.  
  233.  
  234.     function StringDialog (prompt: Str255; var response: Str255): Boolean;
  235.  
  236.     { Get a string from the user. }
  237.  
  238.         var
  239.             theDialog: TStringDialog;
  240.             theItem: Integer;
  241.  
  242.     begin
  243.         StringDialog := false;
  244.         New(theDialog);
  245.         if theDialog <> nil then
  246.             begin
  247.                 theDialog.IStringDialog(prompt, response);
  248.                 theItem := theDialog.Show;
  249.                 if theItem = OKButton then
  250.                     begin
  251.                         theDialog.GetData(response);
  252.                         StringDialog := true
  253.                     end;
  254.                 theDialog.Free
  255.             end
  256.     end;
  257.  
  258.  
  259.     function FontSizeDialog (var fontSize: Integer): Boolean;
  260.  
  261.     { Get new font size. }
  262.  
  263.         var
  264.             theDialog: TFontSizeDialog;
  265.             theItem: Integer;
  266.  
  267.     begin
  268.         FontSizeDialog := false;
  269.         New(theDialog);
  270.         if theDialog <> nil then
  271.             begin
  272.                 theDialog.IFontSizeDialog(fontSize);
  273.                 theItem := theDialog.Show;
  274.                 if theItem = OKButton then
  275.                     begin
  276.                         theDialog.GetData(fontSize);
  277.                         FontSizeDialog := true
  278.                     end;
  279.                 theDialog.Free
  280.             end
  281.     end;
  282.  
  283.  
  284.     function MarginsDialog (var margins: MarginRecord): Boolean;
  285.  
  286.     { Get new page margins. }
  287.  
  288.         var
  289.             theDialog: TMarginsDialog;
  290.             theItem: Integer;
  291.  
  292.     begin
  293.         MarginsDialog := false;
  294.         New(theDialog);
  295.         if theDialog <> nil then
  296.             begin
  297.                 theDialog.IMarginsDialog(margins);
  298.                 theItem := theDialog.Show;
  299.                 if theItem = OKButton then
  300.                     begin
  301.                         theDialog.GetData(margins);
  302.                         MarginsDialog := true
  303.                     end;
  304.                 theDialog.Free
  305.             end;
  306.     end;
  307.  
  308.  
  309. end.